/*
The following variables will need to be provided
before including this file:

app_call_url
uid
pid

*/

var tag_dialog = new Dialog(Dialog.DIALOG_POP);
var tags_div = document.getElementById("photo_tags");

// Make the call to tag_photo.php to add the tags
function add_tags() {
	
	// Get the form and serialize the parameters
	var tag_form = document.getElementById("tag_form");
	var params = tag_form ? tag_form.serialize() : null;
	
	// Generate the URL
	var url = callback_url + "/tag_photo.php?uid=" + uid + "&pid=" + pid;
	
	// Create the ajax object and make the call
	var add_ajax = new Ajax();
	add_ajax.responseType = Ajax.JSON;
	add_ajax.ondone = function(data) {
		show_close_dialog(data);
	};
	add_ajax.post(url, params);
	
}

// Remove the specified tag
function remove_tag(tagid, token) {
	
	// Generate the URL
	var url = callback_url + "/tag_photo.php?uid=" + uid + "&pid=" + pid + "&remove&tagid=" + tagid + "&token=" + token;
	
	// Create the ajax object and make the call
	var remove_ajax = new Ajax();
	remove_ajax.responseType = Ajax.FBML;
	remove_ajax.ondone = function(data) {
		tags_div.setInnerFBML(data);
	}
	remove_ajax.post(url, "");
	
}

// Show the returned message
function show_close_dialog(data) {
	tag_dialog.showMessage(data.title, data.fbml_content, data.button);
	tag_dialog.onconfirm = function() {
		update_tag_list();
		tag_dialog.hide();
	};
}

// Show the tag selection dialog
function show_tag_dialog() {
	// Make the ajax call when the user clicks tag
	tag_dialog.onconfirm = function() {
		add_tags();
	};
	
	// Hide if the user cancels
	tag_dialog.oncancel = function() {
		tag_dialog.hide();
	};
	
	// Show the choice
	tag_dialog.showChoice("Tag This Photo", tag_dialog_content, "Tag", "Cancel");
}

// Update the list of tagged users
function update_tag_list() {
	var tags_ajax = new Ajax();
	tags_ajax.responseType = Ajax.FBML;
	tags_ajax.ondone = function(data) {
		tags_div.setInnerFBML(data);
		tags_div.removeClassName("hide");
	}
	tags_ajax.post(callback_url + "/tag_photo.php?uid=" + uid + "&pid=" + pid + "&refresh", "");
}